function export_fi(varargin)

% % export the fi's of a group of selected records. 
% assumes on a common current pulse base... 
% 
% based on basic template file for data analysis routines.
% gets the current datac window selection, and does the analysis
% for all selected record groups/blocks
% automatically refreshes the data arrays from the disk file if necessary
% and also shows how to generate a figure.
% Plugin_template2 shows how to  access the main database rather than
% specific data files/data (in other words, how to summarize analyzed data)
% 8/06 P. Manis

% the main routine is just a wrapper to handle the multiple selection and
% catch execution errors.
%
% Note: when testing, set the testflag to 1 so that we DO NOT do try/catch.
% you need to do this to catch the errors in the _go routine.
%


sf = getmainselection; % read the list of selected traces
testflag = 1;
nid = 10;
length(sf)
if(sf > 0)
    pflag = getplotflag; % read teh plot flag (used in the routine template2)
    
    QueMessage('Export FI', 1); % clear the que
    y = [];
    for i = 1:length(sf)
        if(testflag == 0)
%            try
                template_go(sf(i),  i);
%            catch
%                fprintf(1, 'Plugin Export FI Failed execution for sf = %d\n  ***[File: %s, records: %d - %d ]\n', ...
%                    sf(i), CONTROL(sf(i)).filename, CONTROL(sf(i)).recbeg, CONTROL(sf(i)).recend);
%            end;
        else
            u = template_go(sf(i), i); % also turn off plot flag when testing

            if (i == 1)
                y = u;
            else
                y = [y ; u];
            end;
        end;

    end;
end;
% y = y'
hout = fopen(sprintf('/users/pbmanis/desktop/exportfi%d.dat', nid), 'w');
k = 1;
for i = 1:2:size(y, 2)
    fprintf(hout, '\t"I%dc%d"\t"Nsp%dc%d"', nid, k, nid, k); % titles of variables
    k = k + 1;
end
fprintf(hout, '\r\n');
for i = 1:size(y, 1)
    for j = 1:2:size(y,2)
        fprintf(hout, '\t%f\t%f', y(i,j), y(i,j+1));
    end;
    fprintf(hout, '\r\n');
end;
fclose(hout);



%--------------The real analysis routine------------------

function u = template_go(sf,  index)

% access to all of the data and parameters
global CONTROL

if(isempty(CONTROL(sf).iv))
    QueMessage('IV not analyzed for %s', CONTROL(sf).filename);
    return;
end;

x=CONTROL(sf).iv;
inj = x.mean(1,:);
u = [x.mean(1,:); x.nr_spk]'
return;

%if(index == 1)
%    fprintf(1, 'File\t I\n');
%end;
fprintf(1, '%s,', CONTROL(sf).filename);
for i = 1:length(inj)
    fprintf(1, '%8.3f,',inj(i));
end;
fprintf(1, '\n ,');

for i = 1:length(inj);
    fprintf(1, '%8d,', x.nr_spk(i));
end;
fprintf(1, '\n');



